/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */

Ext.namespace("User");

/**
 * User edit dialog.
 *
 * @param config the configuration.
 */
User.UserDialog = function(config) {
	User.UserDialog.superclass.constructor.call(this, config);
};

Ext.extend(User.UserDialog, Ext.Window, {

	user: null,

	initComponent: function() {

		Ext.apply(this, {
			renderTo: Ext.getBody(),
			modal: true,
			iconCls: 'icon-user-edit',
			title: MSG['user.data'],
			bodyStyle:'padding:5px'
		});

		this.items = new Ext.FormPanel({
			width: '100%',
			labelWidth: 75,
			frame:true,
			bodyStyle: 'padding: 5px',
			defaults: {
				width: 230
			},
			defaultType: 'textfield',
			items: [{
				xtype: 'hidden',
				name: 'id',
				value: this.user.id
			}, {
				xtype: 'textfield',
				name: 'login',
				fieldLabel: MSG['user.login'],
				disabled: true,
				allowBlank: false,
				value: this.user.login
			},{
				xtype: 'textfield',
				name: 'fullname',
				fieldLabel: MSG['user.fullname'],
				maxLength: 30,
				allowBlank: false,
				value: this.user.fullname,
				vtype :'fullName'
			},{
				xtype: 'textfield',
				name: 'email',
				fieldLabel: MSG['user.email'],
				maxLength: 50,
				allowBlank: false,
				vtype: 'email',
				value: this.user.email
			}, new Common.LanguageCombo({
				hiddenName: 'language',
				value: this.user.language
			})],
			buttons: [{
				text: MSG['button.save'],
				iconCls: 'icon-ok',
				handler: this.onSave,
				scope: this
			},{
				text: MSG['button.cancel'],
				iconCls: 'icon-cancel',
				handler: this.onCancel,
				scope: this
			}]});

		User.UserDialog.superclass.initComponent.call(this);
	},

	onSave: function() {

		var theForm = this.items.items[0].getForm();

		theForm.submit({
			url: CONTEXT_PATH + '/secure/myaccount.json',
			method: 'POST',
			clientValidation: true,
			waitTitle: MSG['wait.title'],
			waitMsg: MSG['wait.message'],
			success: this.onUpdateSuccess,
			failure: this.onUpdateError,
			scope: this
		});

	},

	onCancel: function() {
		this.close();
	},

	onUpdateSuccess: function(form, action) {
		// Update the user
		Main.app.setUser(action.result.data);
		// Close
		this.close();
		notifySuccess(MSG['user.update'], MSG['update.success']);
	},

	onUpdateError: function(/*form, action*/) {
		this.close();
		notifyError(MSG['user.update'], MSG['update.error']);
	}

});


MODULES['1_user'] = {

	menu: null,

	init: function() {

		// Initialize common menu
		this.menu = new Ext.menu.Menu({
			items: [{
				text: MSG['user.data'],
				iconCls: 'icon-user-edit',
				handler: this.onEditUser,
				scope: this
			},{
				text: MSG['user.password'],
				iconCls: 'icon-key-edit',
				handler: this.onChangePassword,
				scope: this
			},{
				text: MSG['button.logout'],
				iconCls: 'icon-logout',
				handler:	function() {
					window.location = CONTEXT_PATH + "/logout.do";
				},
				scope: this
			}]
		});


		// Add menu to toolbar
		Main.app.mainToolbar.add({
			text: MSG['options.main'],
			iconCls: 'icon-blist',
			menu: this.menu
		});
	},

	onEditUser: function() {
		var userDialog = new User.UserDialog({ user: Main.app.getUser() });
		userDialog.show();
	},

	onChangePassword: function() {
		var passwordDialog = new Common.PasswordDialog({user: USER});
		passwordDialog.show();
	}

};